<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/3/23
 * Time: 14:11
 */

namespace app\zucai\home;

use app\index\controller\Home;
use think\Db;
use think\Exception;

/**
 * Class User
 * @package app\zucai\home
 * 登录注册接口
 */
class User extends Home
{

    /**
     * @return \think\response\Json
     * 用户注册接口
     * gxq <know1111@vip.qq.com>
     */
    public function register()
    {
        $ispost = $this->request->isPost();
        if (!$ispost) return returnError('请使用POST方式请求');

        $post = $this->request->post();
        if (!$post) return returnError('未获取到POST数据');

        //if (!(isset($post['user']) && $post['user'])) return returnError('未获取到user');
        if (!(isset($post['password']) && $post['password'])) return returnError('未获取到password');
        //if (!(isset($post['paypassword']) && $post['paypassword'])) return returnError('未获取到paypassword');
        if (!(isset($post['name']) && $post['name'])) return returnError('未获取到name');
        //if (!(isset($post['identity']) && $post['identity'])) return returnError('未获取到identity');
        if (!(isset($post['mobile']) && $post['mobile'])) return returnError('未获取到mobile');
        if (!(isset($post['sign']) && $post['sign'])) return returnError('未获取到sign');

        //$user = trimall($post['user']);//真实姓名
        $password = trimall($post['password']);//密码
        //$paypassword = trimall($post['paypassword']);//支付密码
        $name = trimall($post['name']);//昵称
        //$identity = trimall($post['identity']);//身份证
        $mobile = trimall($post['mobile']);//手机号
        $sign = trimall($post['sign']);//签名

        //if (!verName($user)) return returnError('user验证失败');
        if (!verpassword($password)) return returnError('password验证失败');
        //if (!verpassword($paypassword)) return returnError('paypassword验证失败');
        if (strlen($name) < 1 || strlen($name) > 30) return returnError('name验证失败');
        //if (!validateIDCard($identity)) return returnError('identity验证失败');
        if (!verPhone2($mobile)) return returnError('mobile验证失败');
        $agent = Db::name('admin_user')->where('sign', $sign)->find();
        if (!$agent) return returnError('sign验证失败');

        if (Db::name('user')->where('name', $name)->where('uid',$agent['id'])->find()) return returnError('您的昵称已被注册，请更换昵称');

        $add['uid'] = $agent['id'];
        //$add['user'] = $user;
        $add['password'] = strtoupper(md5($password));
        //$add['paypassword'] = strtoupper(md5($paypassword));
        $add['name'] = $name;
        //$add['identity'] = $identity;
        $add['mobile'] = $mobile;
        $add['avatar'] = rand(1, 11);
        $add['money'] = 0;
        $add['blockmoney'] = 0;
        $add['allmoney'] = 0;
        $add['timoney'] = 0;
        $add['create_time'] = time();
        $add['last_login_time'] = 0;
        $add['status'] = 1;

        if (Db::name('user')->insert($add)) {
            return returnOk('注册成功');
        } else {
            return returnError('注册失败');
        }
    }

    /**
     * @return \think\response\Json
     * 登录接口
     * gxq <know1111@vip.qq.com>
     */
    public function login()
    {
        $ispost = $this->request->isPost();
        if (!$ispost) return returnError('请使用POST方式请求');

        $post = $this->request->post();
        if (!$post) return returnError('未获取到POST数据');

        if (!(isset($post['type']) && $post['type'])) return returnError('未获取到type');
        if (!(isset($post['sign']) && $post['sign'])) return returnError('未获取到sign');
        if (!(isset($post['mobile']) && $post['mobile'])) return returnError('未获取到mobile');

        $type = trimall($post['type']);//登录类型 1:账号密码登录  2:短信验证码登录
        $sign = trimall($post['sign']);//签名
        $mobile = trimall($post['mobile']);//手机号

        if (!verPhone2($mobile)) return returnError('mobile验证失败');
        $agent = Db::name('admin_user')->where('sign', $sign)->find();

        if (!$agent) return returnError('sign验证失败');

        switch ($type) {
            case 1://密码登录
                if (!(isset($post['password']) && $post['password'])) return returnError('未获取到password');
                $password = trimall($post['password']);//密码
                if (!verpassword($password)) return returnError('password验证失败');
                $where['password'] = strtoupper(md5($password));
                break;
            case 2://验证码登录
                if (!(isset($post['code']) && $post['code'])) return returnError('未获取到code');
                $code = trimall($post['code']);//验证码
                $data_post['type'] = 2;
                $data_post['code'] = $code;
                $data_post['mobile'] = $mobile;


                $curl = curl_post_https($_SERVER['SERVER_NAME'] . ':' . $_SERVER["SERVER_PORT"] . url(' '), $data_post);
				$curl=trim($curl, "\xEF\xBB\xBF");
                $curl = json_decode($curl);
                if ($curl->code == 200) {
                    $where = 1;
                } else {
                    return returnError($curl->message);
                }
                break;
            default:
                return returnError('违规操作');
        }

        $user = Db::name('user')
				->where('uid', $agent['id'])
				->where('mobile', $mobile)
				->where($where)
				->field('id,user,name,mobile,avatar,money,create_time,last_login_time,content,paypassword,identity,status')
				->find();

        if ($user) {
			if($user['status']==0) return returnError('您的帐户已被禁用,如有疑问请联系管理员');
            if (strlen($user['paypassword']) == 0) {
                $user['pay'] = 0;
            } else {
                $user['pay'] = 1;
            }
            unset($user['paypassword']);
            if ($user['avatar'] >= 1 && $user['avatar'] <= 11) $user['avatar'] = get_file_path($user['avatar']);
			
			$data_list = Db::name('order')
                    ->where('isdraw', 2)
                    ->where('uid', $user['id'])
                    ->select();
                $allmoney = 0;
                foreach ( $data_list as $k => $v) {
                    $allmoney += $v['mode'];
                }
                $cont=Db::name('order')
                    ->where('isdraw', 'in','2,3')
                    ->where('uid', $user['id'])
                    ->count();
                $user['allplay']=$cont;     //总投注次数
                $user['allmoney']=$allmoney; //总获奖金额
                $user['zhong']=count($data_list); //总中奖次数

                //7天内的投注情况
                $data=Db::name('order')
                    ->field('id,isdraw,oddtime')
                    ->order('oddtime')
                    ->where('isdraw', 'in','2,3')
                    ->where('oddtime', '>=',date('Y-m-d', strtotime('-7 days')))
                    ->where('uid', $user['id'])
                    ->select();
                $data2=Db::name('order')
                    ->where('isdraw', 2)
                    ->where('oddtime', '>=',date('Y-m-d', strtotime('-7 days')))
                    ->where('uid', $user['id'])
                    ->count();
                //计算7天内连中次数
                $flag=0;$max=0;
                for($i=0;$i<count($data);$i++){
                    if($data[$i]['isdraw']==2){
                        $flag++;
                        if($flag>$max){
                            $max=$flag;
                        }
                    }else{
                        $flag=0;
                    }
                }
                $user['timeplay']=count($data);     //7天内总投注次数
                $user['timenumber']=$data2; //7天内总中奖次数
                $user['target']=$max; //7天内连中次数
			
			$login_add['ip']=$_SERVER["REMOTE_ADDR"];
			$login_add['uid']=$user['id'];
			$login_add['username']=$user['user'];
			Db::name('login')->insert($login_add);
            Db::name('user')->where('id', $user['id'])->update(['last_login_time' => time()]);//更新用户登录时间
            return returnOk($user);
        } else {
            return returnError('账号或密码错误');
        }
    }

    /**
     * @return \think\response\Json
     * 发送验证码
     * gxq <know1111@vip.qq.com>
     */
    public function sendsms()
    {
        $ispost = $this->request->isPost();
        if (!$ispost) return returnError('请使用POST方式请求');

        $post = $this->request->post();
        if (!$post) return returnError('未获取到POST数据');

        if (!(isset($post['mobile']) && $post['mobile'])) return returnError('未获取到mobile');
        if (!(isset($post['type']) && $post['type'])) return returnError('未获取到type');

        $mobile = trimall($post['mobile']);//手机号
        $type = trimall($post['type']);//请求验证码类型 1:注册 2:登录 3:忘记密码 4:绑定银行卡 5:手机换绑

        if (!verPhone2($mobile)) return returnError('mobile验证失败');

        $add['code'] = rand(100000, 999999);
        $add['mobile'] = $mobile;
        $add['create_time'] = time();
        switch ($type) {
            case 1:
                $add['content'] = "您的注册验证码是：" . $add['code'] . "，请不要把验证码泄露给其他人！3分钟内有效。【天红网络】";
                $add['type'] = $type;
                break;
            case 2:
                $add['content'] = "您的本次登录校验码为：" . $add['code'] . ",动态码有效时间为180秒，请不要告诉其他人哦。【天红网络】";
                $add['type'] = $type;
                break;
            case 3:
                $add['content'] = "您好，您正在使用网站的忘记密码找回操作，本次验证码为：" . $add['code'] . ", 请勿泄露。【天红网络】";
                $add['type'] = $type;
                break;
            case 4:
                $add['content'] = "您好,您正在使用天红体彩的提现到银行卡功能,本次验证码为：" . $add['code'] . ",如果不是本人操作，请赶快联系管理员并修改密码。【天红网络】";
                $add['type'] = $type;
                break;
            case 5:
                $add['content'] = "亲爱的用户您好，你正在使用手机换绑操作，验证码：" . $add['code'] . "，如非本人操作，请立即联系管理员。【天红网络】";
                $add['type'] = $type;
                break;
            default:
                return returnError('违规操作');
        }

        $send_data = send($mobile, $add['content']);

        $xml = xmlToArray($send_data);
        if ($xml['returnstatus'] == 'Success') {
            $add_data = Db::name('code')->insert($add);

            if ($add_data) {
                return returnOk('验证码发送成功');
            } else {
                return returnError('新增验证码数据失败');
            }
        } else {
            return returnError('验证码发送失败');
        }

    }

    /**
     * @return \think\response\Json
     * 验证验证码
     * gxq <know1111@vip.qq.com>
     */
    public function varsms()
    {
        $ispost = $this->request->isPost();
        if (!$ispost) return returnError('请使用POST方式请求');

        $post = $this->request->post();
        if (!$post) return returnError('未获取到POST数据');

        if (!(isset($post['mobile']) && $post['mobile'])) return returnError('未获取到mobile');
        if (!(isset($post['code']) && $post['code'])) return returnError('未获取到code');
        if (!(isset($post['type']) && $post['type'])) return returnError('未获取到type');

        $mobile = trimall($post['mobile']);//手机号
        $code = trimall($post['code']);//验证码
        $type = trimall($post['type']);//请求类型 1:注册 2:登录 3:找回密码 4:提现


        if (!verPhone2($mobile)) return returnError('mobile验证失败');

        $code = Db::name('code')->where('code', $code)->where('mobile', $mobile)->where('type', $type)->find();

        if ($code) {
            if (time() - $code['create_time'] <= 180) {
                return returnOk('验证成功');
            } else {
                return returnError('验证码已失效,请重新获取');
            }
        } else {
            return returnError('验证码错误');
        }
    }


    /**
     * @return \think\response\Json
     * 找回密码
     * gxq <know1111@vip.qq.com>
     */
    public function seekPassword()
    {
        $ispost = $this->request->isPost();
        if (!$ispost) return returnError('请使用POST方式请求');

        $post = $this->request->post();
        if (!$post) return returnError('未获取到POST数据');

        if (!(isset($post['mobile']) && $post['mobile'])) return returnError('未获取到mobile');
        if (!(isset($post['code']) && $post['code'])) return returnError('未获取到code');
        if (!(isset($post['sign']) && $post['sign'])) return returnError('未获取到sign');
        if (!(isset($post['password']) && $post['password'])) return returnError('未获取到password');
        if (!(isset($post['type']) && $post['type'])) return returnError('未获取到type');

        $code = trimall($post['code']);//验证码
        $sign = trimall($post['sign']);//签名
        $mobile = trimall($post['mobile']);//手机号
        $password = trimall($post['password']);//密码
        $type = trimall($post['type']);//请求类型  1:找回登录密码  2:找回支付密码

        if (!verPhone2($mobile)) return returnError('mobile验证失败');
        $agent = Db::name('admin_user')->where('sign', $sign)->find();
        if (!$agent) return returnError('sign验证失败');
        if (!verpassword($password)) return returnError('password验证失败');

        $data_post['type'] = 3;
        $data_post['code'] = $code;
        $data_post['mobile'] = $mobile;

        $curl = curl_post_https($_SERVER['SERVER_NAME'] . ':' . $_SERVER["SERVER_PORT"] . url('varsms'), $data_post);
         $curl=trim($curl, "\xEF\xBB\xBF");
		$curl = json_decode($curl);
        if ($curl->code == 200) {
            $user = Db::name('user')->where('uid', $agent['id'])->where('mobile', $mobile)->find();

            switch ($type) {
                case 1:
                    $yzpassword = $user['password'];
                    $upadate['password'] = strtoupper(md5($password));
                    break;
                case 2:
                    $yzpassword = $user['paypassword'];
                    $upadate['paypassword'] = strtoupper(md5($password));
                    break;
                default:
                    return returnError('违规操作');
            }
            if ($yzpassword == strtoupper(md5($password))) {
                return returnError('要修改的密码不能和当前密码相同');
            }
            if (Db::name('user')->where('uid', $agent['id'])->where('mobile', $mobile)->where('id', $user['id'])->update($upadate)) {
                return returnOk('密码修改成功');
            } else {
                return returnError('密码修改失败');
            }
        } else {
            return returnError($curl->message);
        }
    }

    /**
     * @return \think\response\Json
     * 修改密码
     * gxq <know1111@vip.qq.com>
     */
    public function revisePwd()
    {
        $ispost = $this->request->isPost();
        if (!$ispost) return returnError('请使用POST方式请求');

        $post = $this->request->post();
        if (!$post) return returnError('未获取到POST数据');

        if (!(isset($post['user_id']) && $post['user_id'])) return returnError('未获取到user_id');
        if (!(isset($post['password']) && $post['password'])) return returnError('未获取到password');
        if (!(isset($post['new_password']) && $post['new_password'])) return returnError('未获取到new_password');
        if (!(isset($post['type']) && $post['type'])) return returnError('未获取到type');

        $user_id = trimall($post['user_id']);//用户ID
        $password = trimall($post['password']);//旧密码
        $new_password = trimall($post['new_password']);//新密码
        $type = trimall($post['type']);//请求类型  1:修改登录密码  2:修改支付密码

        if (!verpassword($password)) return returnError('password验证失败');
        if (!verpassword($new_password)) return returnError('new_password验证失败');
        if ($password == $new_password) return returnError('要修改的密码不能和当前密码相同');

        switch ($type) {
            case 1:
                $yzpassword['password'] = strtoupper(md5($password));
                $update['password'] = strtoupper(md5($new_password));
                break;
            case 2:
                $yzpassword['paypassword'] = strtoupper(md5($password));
                $update['paypassword'] = strtoupper(md5($new_password));
                break;
            default:
                return returnError('违规操作');
        }

        $user = Db::name('user')->where('id', $user_id)->where($yzpassword)->find();
        if (!$user) return returnError('密码错误');

        if (Db::name('user')->where('id', $user_id)->update($update)) {
            return returnOk('密码修改成功');
        } else {
            return returnError('密码修改失败');
        }
    }

    /**
     * @return \think\response\Json
     * 设置支付密码
     * gxq <know1111@vip.qq.com>
     */
    public function setPayPassword()
    {
        $ispost = $this->request->isPost();
        if (!$ispost) return returnError('请使用POST方式请求');

        $post = $this->request->post();
        if (!$post) return returnError('未获取到POST数据');

        if (!(isset($post['user_id']) && $post['user_id'])) return returnError('未获取到user_id');
        if (!(isset($post['paypassword']) && $post['paypassword'])) return returnError('未获取到paypassword');

        $user_id = trimall($post['user_id']);//用户ID
        $paypassword = trimall($post['paypassword']);//支付密码
        if (!verpassword($paypassword)) return returnError('paypassword验证失败');

        $user = Db::name('user')->where('id', $user_id)->find();
        if (!$user) return returnError('违规操作');
        if ($user['paypassword'] != "") return returnError('已设置支付密码');
        if (Db::name('user')->where('id', $user_id)->update(['paypassword' => strtoupper(md5($paypassword))])) {
            return returnOk('支付密码设置成功');
        } else {
            return returnError('支付密码设置失败');
        }
    }

    /**
     * @return \think\response\Json
     * 绑定银行卡
     * gxq <know1111@vip.qq.com>
     */
    public function bindCard()
    {
        $ispost = $this->request->isPost();
        if (!$ispost) return returnError('请使用POST方式请求');

        $post = $this->request->post();
        if (!$post) return returnError('未获取到POST数据');

        if (!(isset($post['user_id']) && $post['user_id'])) return returnError('未获取到user_id');
        if (!(isset($post['username']) && $post['username'])) return returnError('未获取到username');
        if (!(isset($post['type']) && $post['type'])) return returnError('未获取到type');
        if (!(isset($post['cardname']) && $post['cardname'])) return returnError('未获取到cardname');
        if (!(isset($post['carnumber']) && $post['carnumber'])) return returnError('未获取到carnumber');

        $user_id = trimall($post['user_id']);//用户ID
        $user = trimall($post['username']);//真实姓名
        $type = trimall($post['type']);//银行卡类型
        $cardname = trimall($post['cardname']);//银行名称
        $carnumber = trimall($post['carnumber']);//银行卡号

        if (!verName($user)) return returnError('真实姓名验证失败');
        if (!verName($user)) return returnError('真实姓名验证失败');
        if (!luhm($carnumber)) return returnError('银行卡号验证失败');

        $userinfo=Db::name('user')->where('id',$user_id)->find();
        if (!$userinfo['user']||!$userinfo['identity']) return returnError('请先实名认证');

        $bank_id=Db::name('bank')->where('bank_name',$cardname)->value('id');
        if ($bank_id)
        {
            $add['bank_id']=$bank_id;
        }
        else{
            $add['bank_id']=9;
        }
        $add['uid'] = $user_id;
        $add['username'] = $user;
        $add['type'] = $type;
        $add['cardname'] = $cardname;
        $add['carnumber'] = $carnumber;

        if (Db::name('user_card')->where($add)->find()) return returnError('该银行卡已存在');
        $add['status'] = 1;
        $add['create_time']=date('Y-m-d',time());

        if (Db::name('user_card')->insert($add)) {
            return returnOk('绑定银行卡成功');
        } else {
            return returnError('绑定银行卡失败');
        }
    }

    /**
     * @return \think\response\Json
     *解绑银行卡
     * gxq <know1111@vip.qq.com>
     */
    public function untieCard()
    {
        $ispost = $this->request->isPost();
        if (!$ispost) return returnError('请使用POST方式请求');

        $post = $this->request->post();
        if (!$post) return returnError('未获取到POST数据');

        if (!(isset($post['user_id']) && $post['user_id'])) return returnError('未获取到user_id');
        if (!(isset($post['card_id']) && $post['card_id'])) return returnError('未获取到card_id');

        $user_id = trimall($post['user_id']);//用户ID
        $card_id = trimall($post['card_id']);//银行卡ID

        $del['uid'] = $user_id;
        $del['id'] = $card_id;

        if (!Db::name('user_card')->where($del)->find()) return returnError('该银行卡不存在');


        if (Db::name('user_card')->where($del)->delete()) {
            return returnOk('解绑银行卡成功');
        } else {
            return returnError('解绑银行卡失败');
        }
    }

    /**
     * @return \think\response\Json
     * 查询用户银行卡
     * gxq <know1111@vip.qq.com>
     */
    public function queryCard()
    {
        $ispost = $this->request->isPost();
        if (!$ispost) return returnError('请使用POST方式请求');

        $post = $this->request->post();
        if (!$post) return returnError('未获取到POST数据');

        if (!(isset($post['user_id']) && $post['user_id'])) return returnError('未获取到user_id');

        $user_id = trimall($post['user_id']);//用户ID

        $card = Db::name('user_card')->alias('a')->where('uid', $user_id)->where('status', 1)->join('zc_bank b','a.bank_id=b.id')->field('a.id,a.username,a.type,a.cardname,a.carnumber,a.create_time,b.logo')->select();

        foreach ($card as $k=>$v) {
            $card[$k]['logo']=get_file_path($v['logo']);
        }
        return returnOk($card);
    }

    /**
     * @return \think\response\Json
     * 提现列表
     * gxq <know1111@vip.qq.com>
     */
    public function putCashList()
    {
        $ispost = $this->request->isPost();
        if (!$ispost) return returnError('请使用POST方式请求');

        $post = $this->request->post();
        if (!$post) return returnError('未获取到POST数据');

        if (!(isset($post['user_id']) && $post['user_id'])) return returnError('未获取到user_id');

        $user_id = trimall($post['user_id']);//用户ID

        $user=Db::name('user')->where('id',$user_id)->find();

        if (!$user) return returnError('该用户不存在');

        $cash=Db::name('put_cash')
            ->alias('a')
            ->join('zc_user_card b','a.card_id=b.id')
            ->join('zc_bank c','b.bank_id=c.id')
            ->field('a.id,a.user_id,a.card_id,money,a.create_time,a.status,b.username,b.type,b.cardname,b.carnumber,c.logo')
            ->where('a.user_id',$user_id)
			->order('create_time desc')
            ->select();

        foreach ($cash as $k=>$v) {
            $cash[$k]['logo']=get_file_path($v['logo']);
        }
        return returnOk($cash);
    }


    /**
     * @return \think\response\Json
     * 提现申请
     * gxq <know1111@vip.qq.com>
     */

    public function putCash()
    {
        $ispost = $this->request->isPost();
        if (!$ispost) return returnError('请使用POST方式请求');

        $post = $this->request->post();
        if (!$post) return returnError('未获取到POST数据');

        if (!(isset($post['user_id']) && $post['user_id'])) return returnError('未获取到user_id');
        if (!(isset($post['card_id']) && $post['card_id'])) return returnError('未获取到card_id');
        if (!(isset($post['money']) && $post['money'])) return returnError('未获取到money');
        if (!(isset($post['paypassword']) && $post['paypassword'])) return returnError('未获取到paypassword');

        $user_id = trimall($post['user_id']);//用户ID
        $card_id = trimall($post['card_id']);//银行卡ID
        $money = trimall($post['money']);//提现金额
        $paypassword = trimall($post['paypassword']);//支付密码
        if (intval($money)) {
            $money = intval($money);
        } else {
            return returnError('金额格式不正确');
        }

        if (!verpassword($paypassword)) return returnError('支付密码格式不正确');
        $user = Db::name('user')->where('id', $user_id)->find();
        if (!$user) return returnError('该用户不存在');

            if (!$user['paypassword']) return returnError('未设置支付密码');
            if (strtoupper(md5($paypassword)) != $user['paypassword']) return returnError('支付密码不正确');
            if ($money < 20 || $money > 50000) return returnError('单次提现金额不能低于20元，不能高于50000元');
            if ($money > $user['tx_money']) return returnError('可提现余额不足');
            if ($money > $user['money']) return returnError('余额不足');
            if (Db::name('user_card')->where('id', $card_id)->value('uid') != $user_id) return returnError('违规操作');

            Db::startTrans();//事务开始

            try {
                if (!Db::name('user')->where('id', $user['id'])->update(['tx_money' => $user['tx_money'] - $money, 'money' => $user['money'] - $money, 'timoney' => $user['timoney'] + $money])) throw new Exception();
                $add['order_id'] = date('Ymd') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT) . $user['id'];
                $add['uid'] = $user['id'];
                $add['type'] = 4;
                $add['caizhong'] = '足彩';
                $add['caicontent'] = '用户\'' . $user['name'] . '\'的提现申请';
                $add['username'] = $user['user'];
                $add['name'] = $user['name'];
                $add['money'] = $user['money'];
                $add['lastmoney'] = $user['money'] - $money;
                $add['dmoney'] = $user['blockmoney'];
                $add['time'] = date('Y-m-d H:i:s');
                if (!Db::name('account')->insert($add)) throw new Exception();
                if (!Db::name('put_cash')->insert([
                    'user_id' => $user['id'],
                    'card_id' => $card_id,
                    'money' => $money,
                    'update_time' => date('Y-m-d H:i:s'),
                    'status' => 1
                ])
                ) throw new Exception();
                Db::commit();
                return returnOk('提现申请成功');
            } catch (Exception $e) {
                Db::rollback();
                return returnError('提现申请失败');
            }
    }
	
	 /**
     * @return \think\response\Json
     * 用户充值记录查询
     * gxq <know1111@vip.qq.com>
     */
    public function getCashList()
    {
        $ispost = $this->request->isPost();
        if (!$ispost) return returnError('请使用POST方式请求');

        $post = $this->request->post();
        if (!$post) return returnError('未获取到POST数据');

        if (!(isset($post['user_id']) && $post['user_id'])) return returnError('未获取到user_id');

        $user_id = trimall($post['user_id']);//用户ID

        $user=Db::name('user')->where('id',$user_id)->find();

        if (!$user) return returnError('该用户不存在');

        $cash=Db::name('user_put')
            //->join('zc_user_card b','a.card_id=b.id')
            //->join('zc_bank c','b.bank_id=c.id')
            ->field('uid',true)
            ->where('uid',$user_id)
            ->order('time desc')
            ->select();

        foreach ($cash as $k=>$v) {
            if ($v['status']==0&&time()-strtotime($v['time'])>=420){
                $cash[$k]['status']=2;
            }
        }
        return returnOk($cash);
    }

    /**
     * @return \think\response\Json
     * 图片上传
     * gxq <know1111@vip.qq.com>
     */
    public function uploadPicture()
    {
        $ispost = $this->request->isPost();
        if (!$ispost) return returnError('请使用POST方式请求');

        $post = $this->request->post();
        if (!$post) return returnError('未获取到POST数据');

        if (!(isset($post['user_id']) && $post['user_id'])) return returnError('未获取到user_id');
        if (!(isset($_FILES['head']) && $_FILES['head'])) return returnError('未获取到head');
        $user_id = trimall($post['user_id']);//用户ID
        if (!Db::name('user')->where('id', $user_id)->find()) $this->error('该用户不存在');

        $head = $_FILES['head'];
        $img = verImage($head);
        if ($img['code'] != 200) return returnError($img['message']);

        if (!Db::name('user')->where('id', $user_id)->update(['avatar' => '/' . $img['message']])) return returnError('头像上传失败');

        return returnOk('/' . $img['message']);
    }


    /**
     * @return \think\response\Json
     * 手机换绑
     * gxq <know1111@vip.qq.com>
     */
    public function replaceBind()
    {
        $ispost = $this->request->isPost();
        if (!$ispost) return returnError('请使用POST方式请求');

        $post = $this->request->post();
        if (!$post) return returnError('未获取到POST数据');

        if (!(isset($post['user_id']) && $post['user_id'])) return returnError('未获取到user_id');
        if (!(isset($post['code']) && $post['code'])) return returnError('未获取到code');
        if (!(isset($post['mobile']) && $post['mobile'])) return returnError('未获取到mobile');


        $code = trimall($post['code']);//验证码
        $mobile = trimall($post['mobile']);//新手机号
        $user_id = trimall($post['user_id']);//用户ID

        $user=Db::name('user')->where('id', $user_id)->find();
        if (!$user) return returnError('该用户不存在');
        if (!verPhone2($mobile)) return returnError('mobile验证失败');
        if ($mobile==$user['mobile']) return returnError('手机号不能和旧手机号重复');

        $data_post['type'] = 5;
        $data_post['code'] = $code;
        $data_post['mobile'] = $mobile;
        $curl = curl_post_https($_SERVER['SERVER_NAME'] . ':' . $_SERVER["SERVER_PORT"] . url('varsms'), $data_post);
         $curl=trim($curl, "\xEF\xBB\xBF");
		$curl = json_decode($curl);
        if ($curl->code != 200) {
            return returnError($curl->message);
        } else {
            if (!Db::name('user')->where('id', $user_id)->update(['mobile' => $mobile])) return returnError('换绑失败');

            return returnOk('换绑成功');
        }
    }

    /**
     * @return \think\response\Json
     * 实名认证
     * gxq <know1111@vip.qq.com>
     */
    public function userAuth()
    {
        $ispost = $this->request->isPost();
        if (!$ispost) return returnError('请使用POST方式请求');

        $post = $this->request->post();
        if (!$post) return returnError('未获取到POST数据');

        if (!(isset($post['user_id']) && $post['user_id'])) return returnError('未获取到user_id');
        if (!(isset($post['user']) && $post['user'])) return returnError('未获取到user');
        if (!(isset($post['identity']) && $post['identity'])) return returnError('未获取到identity');

        $identity = trimall($post['identity']);//身份证
        $user = trimall($post['user']);//真实姓名
        $user_id = trimall($post['user_id']);//用户ID

        $userinfo=Db::name('user')->where('id', $user_id)->find();
        if (!$userinfo) return returnError('该用户不存在');

        if (!verName($user)) return returnError('真实姓名验证失败');
        if (!validateIDCard($identity)) return returnError('身份证验证失败');

        if ($userinfo['user']!=null||$userinfo['identity']!=null) return returnError('该账号已实名认证');

        $update['user']=$user;
        $update['identity']=$identity;
        if (!Db::name('user')->where('id',$user_id)->update($update)) return returnError('实名认证失败');

        return returnOk('实名认证成功');
    }
}